package com.ocom.aiot.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.aiot.service.MjAppVerInfoService;
import com.ocom.aiot.vo.accessdevice.AddDeviceVerInfoVo;
import com.ocom.aiot.vo.accessdevice.GetDeviceVerInfoVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.entity.aiot.EntityMjAppVerInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MjAppVerInfoController {

    @Autowired
    private MjAppVerInfoService mjAppVerInfoService;
    /**
     * 2.7.2.获取设备升级版本列表（getDeviceVerInfo）
     * */
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceGetDeviceVerInfo)
    public Result AccessDeviceGetDeviceVerInfo(@Validated @RequestBody GetDeviceVerInfoVo getDeviceVerInfoVo){
        Map<String, Object> map = new HashMap<>();
        try {
            List<EntityMjAppVerInfo> entityMjAppVerInfos = mjAppVerInfoService.AccessDeviceGetDeviceVerInfo(getDeviceVerInfoVo);
            PageInfo<EntityMjAppVerInfo> listPageInfo = new PageInfo<>(entityMjAppVerInfos);
            map.put("total", listPageInfo.getTotal());
            map.put("list", entityMjAppVerInfos);
        }catch (Exception exception){
            log.error("获取设备升级版本列表失败"+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取设备升级版本列表失败");
        }

        return ResultUtil.success(map);
    }


    //2.7.1.保存设备升级版本信息（addDeviceVerInfo）
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceAddDeviceVerInfo)
    public Result AccessDeviceAddDeviceVerInfo(@RequestBody @Validated AddDeviceVerInfoVo addDeviceVerInfoVo){
        if (addDeviceVerInfoVo.getApkType() == null) {
            log.error("设备类型为空值");
            return ResultUtil.error(ResultEnum.Err_203,"设备类型参数为空值");
        }
        if (addDeviceVerInfoVo.getAppVer() == null) {
            log.error("固件版本信息为空值");
            return ResultUtil.error(ResultEnum.Err_203,"固件版本信息为空值");
        }
        if (addDeviceVerInfoVo.getApkFileUrl() == null) {
            log.error("升级文件URL为空值");
            return ResultUtil.error(ResultEnum.Err_203,"升级文件URL为空值");
        }
        try {
            EntityMjAppVerInfo entityMjAppVerInfo = mjAppVerInfoService.GetMjAppVer(addDeviceVerInfoVo.getAppVer());
            if (entityMjAppVerInfo!=null){
                if (addDeviceVerInfoVo.getAppVer().equals(entityMjAppVerInfo.getAppVer())){
                    log.error("固件版本号重复");
                    return ResultUtil.error(ResultEnum.Err_1002,"固件版本号重复");
                }
            }
        }catch (Exception exception){
            log.error("获取设备升级版本列表失败"+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取设备升级版本列表失败");
        }


        try {
            mjAppVerInfoService.AccessDeviceAddDeviceVerInfo(addDeviceVerInfoVo);
        }catch (Exception exception){
            log.error("保存升级版本异常："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"保存升级版本异常");
        }
        return ResultUtil.success();
    }

    /**
     * 2.7.3.删除设备升级版本（deleteDeviceVerInfo）
     *
     * */
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceDeleteDeviceVerInfo)
    public Result AccessDeviceDeleteDeviceVerInfo(Long id){
        try {
            mjAppVerInfoService.AccessDeviceDeleteDeviceVerInfo(id);
        }catch (Exception exception){
            log.error("删除升级版本异常："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"删除升级版本异常");
        }
        return ResultUtil.success();
    }
}
